#!/usr/bin/env python

from glob import glob
from tzutil.day import day2str
from json import load
from os.path import join, dirname, basename

DIR = dirname(__file__)


def convert(fname):
    s = load(open(join(DIR, 'data', fname)))
    li = []
    for k, v in s.items():
        li.append((int(k), float(v)))
    li.sort(key=lambda x: x[0])
    with open(join(DIR, 'txt', fname[:-5] + ".txt"), "w") as f:
        for k, v in li:
            f.write("%s %s\n" % (day2str(k), v / 10000))


for i in glob(join(DIR, 'data/*.json')):
    convert(basename(i))
